package com.zx.servicelearn.service;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.media.app.NotificationCompat;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.RemoteViews;

import com.zx.servicelearn.MainActivity;
import com.zx.servicelearn.R;

import java.util.concurrent.atomic.AtomicBoolean;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.content.ContentValues.TAG;

/**
 * 前台service
 */
public class MyService3 extends Service {
    public MyService3() {
    }

    Notification notification = null;

    Handler mHandler = new Handler() {
        @SuppressLint("NewApi")
        @Override
        public void handleMessage(Message msg) {
            int i = (int) msg.obj;
            switch (msg.what) {
                case 1:
                    notification.bigContentView.setProgressBar(R.id.my_pb, 100, i, false);
                    notification.bigContentView.setTextViewText(R.id.my_tv, "正在下载：" + i + "%");
                    manager.notify(10001, notification);
                    Log.d(TAG, "handleMessage:" + i);
                    break;
                case 2:
//                    notification.contentView.setProgressBar(R.id.my_pb, 100, i, false);
                    notification.bigContentView.setTextViewText(R.id.my_tv, "下载完成");
                    manager.notify(10001, notification);
                    Log.d(TAG, "handleMessage-stop:" + i);
                    break;
            }
        }
    };
    NotificationManager manager;

    @Override
    public void onCreate() {
        super.onCreate();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, FLAG_UPDATE_CURRENT);

//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        RemoteViews remoteViews;
        remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
        remoteViews.setOnClickPendingIntent(R.id.test_bt,pendingIntent);

        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            notification = new Notification.Builder(this)
                    .setCustomContentView(remoteViews)
                    .setCustomBigContentView(remoteViews)
                    .setSmallIcon(android.R.drawable.stat_sys_warning)
    //                        .setColor(getColor(R.color.colorAccent))
    //                .setContentTitle("哈哈哈")
    //                .setContentText("hei hei ")
    //                .setContentIntent(pendingIntent)
                    .setWhen(0)
                    .build();

        } else {
            notification = new Notification(android.R.drawable.stat_sys_warning,
                    "有通知到来", System.currentTimeMillis());
            notification.tickerText = "哈哈哈";
            notification.contentView = remoteViews;
//            n.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容",
//                    pendingIntent);
        }


        startForeground(10001, notification);
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // TODO: 2018/1/18 需要启动下载服务
        new Thread(new DownLoadService()).start();
        return super.onStartCommand(intent, flags, startId);
    }

    private AtomicBoolean mIsServiceDestoryed = new AtomicBoolean(false);//原子性的boolean

    class DownLoadService implements Runnable {
        @Override
        public void run() {
            int i = 0;
            while (!mIsServiceDestoryed.get()) {
                try {
                    if (i >= 100) {
                        mHandler.obtainMessage(2, i).sendToTarget();
                        stopSelf();
                        break;
                    }
                    Thread.sleep(100);
                    i++;
                    // TODO: 2018/1/18 通知更新UI
                    mHandler.obtainMessage(1, i).sendToTarget();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Override
    public void onDestroy() {
        mIsServiceDestoryed.set(true);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
